<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="运动名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入运动名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:sport:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:sport:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:sport:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:sport:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sportList" border stripe @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="运动ID" align="center" prop="id"  width="80" />
      <el-table-column label="运动类型" align="center" prop="type"  width="150">
        <template slot-scope="scope">
          <span>{{ getCategoryName(scope.row.type) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="运动名称" align="center" prop="name" class-name="small-padding fixed-width" />
      <el-table-column label="运动时间" align="center" prop="time"  width="120" />
      <el-table-column label="消耗卡路里" align="center" prop="calorie"  width="120" />
      <el-table-column label="运动图标" align="center" prop="icon"  width="180" >
        <template slot-scope="scope">
          <el-avatar shape="square" :size="30" :src="require('@/assets/sport/' + scope.row.icon)"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column label="背景颜色" align="center" prop="bgColor"  width="80" >
        <template slot-scope="scope">
           <el-card style="width: 40px; height: 40px; background: #eee; border-radius: 10px" :style="{background: scope.row.bgColor}"  ></el-card>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort" width="80"/>
      <el-table-column label="运动介绍" align="center" prop="content"  width="80">
        <template slot-scope="scope">
          <el-popover placement="bottom" title="运动介绍"  width="400" trigger="click"
          :content="scope.row.content">
          <i class="el-icon-info" color="blue"  slot="reference" style="cursor: pointer;"></i>
        </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:sport:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:sport:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改运动项目管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="55%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="运动名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入运动名称" />
        </el-form-item>
        <el-form-item label="运动类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择运动类型">
            <el-option
              v-for="item in categoryOptions"
              :key="item.id"
              :label="item.category"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="运动时间" prop="time">
          <el-input-number v-model="form.time" controls-position="right" :min="30" :max="600">
          </el-input-number>（分钟）
        </el-form-item>
        <el-form-item label="消耗卡路里" prop="calorie">
          <el-input-number v-model="form.calorie" controls-position="right" :min="10" :max="6000">
          </el-input-number>（千卡）
        </el-form-item>
        <el-form-item label="运动图标" prop="icon">
          <el-select v-model="form.icon" placeholder="请选择图标">
            <el-option v-for="dict in dict.type.miniapp_sport_icon" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="背景颜色" prop="bgColor">
          <el-input v-model="form.bgColor" placeholder="请选择颜色" />
          <el-color-picker v-model="form.bgColor"></el-color-picker>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" controls-position="right" :min="1" :max="999">
          </el-input-number>
        </el-form-item>
        <el-form-item label="运动介绍">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSport, getSport, delSport, addSport, updateSport } from "@/api/miniapp/wxSport"
import { listCategory } from "@/api/miniapp/wxSportCategory"

export default {
  name: "Sport",
  dicts: ['miniapp_sport_icon'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 运动项目管理表格数据
      sportList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 运动类别列表
      categoryOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        type: null,
        time: null,
        calorie: null,
        icon: null,
        content: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "运动名称不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "运动类型不能为空", trigger: "change" }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.getCategoryList()
  },
  methods: {
    /** 查询运动项目管理列表 */
    getList() {
      this.loading = true
      listSport(this.queryParams).then(response => {
        this.sportList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        type: null,
        time: null,
        calorie: null,
        sort:1,
        icon: null,
        bgColor: null,
        content: null,
        remark: null,
        createTime: null,
        updateTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加运动项目"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getSport(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改运动项目"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSport(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addSport(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除运动项目管理编号为"' + ids + '"的数据项？').then(function() {
        return delSport(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/sport/export', {
        ...this.queryParams
      }, `sport_${new Date().getTime()}.xlsx`)
    },
    /** 获取运动类别列表 */
    getCategoryList() {
      listCategory().then(response => {
        console.log(response)
        this.categoryOptions = response.rows
      })
    },
    /** 根据类别ID获取类别名称 */
    getCategoryName(categoryId) {
      const category = this.categoryOptions.find(item => item.id === categoryId)
      return category ? category.category : ''
    }
  }
}
</script>
